///*
// * Copyright (C) RT, 2017 by kiterunner_t
// * TO THE HAPPY FEW
// */
//
//package com.zgys.mes.util;
//
//import com.zgys.mes.util.error.RtErrorEnum;
//import com.zgys.mes.util.error.RtException;
//import org.quartz.DateBuilder;
//import org.quartz.JobBuilder;
//import org.quartz.JobDetail;
//import org.quartz.JobKey;
//import org.quartz.ScheduleBuilder;
//import org.quartz.Scheduler;
//import org.quartz.SchedulerException;
//import org.quartz.SchedulerFactory;
//import org.quartz.SimpleScheduleBuilder;
//import org.quartz.Trigger;
//import org.quartz.TriggerBuilder;
//import org.quartz.impl.StdSchedulerFactory;
//
//
//public class TimerScheduler {
//    private final SchedulerFactory SchedulerFac = new StdSchedulerFactory();
//    private Scheduler scheduler;
//
//
//    public TimerScheduler() {
//        try {
//            scheduler = SchedulerFac.getScheduler();
//        } catch (Exception ex) {
//            Log.error("Quartz scheduler get scheduler error");
//            throw new RtException(RtErrorEnum.ERROR_INTERNAL_SERVER);
//        }
//    }
//
//
//    public void start() {
//        try {
//            scheduler.start();
//
//            Log.info("启动定时器");
//
//        } catch (SchedulerException ex) {
//            Log.error("Quartz scheduler start error");
//            throw new RtException(RtErrorEnum.ERROR_INTERNAL_SERVER);
//        }
//    }
//
//
//    public void stop() {
//        try {
//            scheduler.shutdown(true);
//
//        } catch (SchedulerException ex) {
//            Log.error("Quartz scheduler stop error");
//            throw new RtException(RtErrorEnum.ERROR_INTERNAL_SERVER);
//        }
//    }
//
//
//    public void triggerJob(ITimerSchedulerType timerType, String jobName, Object ctx) {
//        String jobGroup = timerType.getGroupName();
//
//        try {
//            JobDetail jobDetail = JobBuilder.newJob(timerType.getJobClass())
//                    .withIdentity(jobName, jobGroup)
//                    .build();
//
//            jobDetail.getJobDataMap().put(jobName, ctx);
//            Trigger trigger = TriggerBuilder.newTrigger()
//                    .withIdentity(jobName, jobGroup)
//                    .startNow()
//                    .build();
//
//            scheduler.scheduleJob(jobDetail, trigger);
//
//        } catch (Exception ex) {
//            Log.error("立即执行job失败，jobName={}", jobName, ex);
//            throw new RtException(RtErrorEnum.ERROR_INTERNAL_SERVER);
//        }
//    }
//
//
//    public void addNonPeriodJob(ITimerSchedulerType timerType, String jobName,
//                                int timeout, Object ctx) {
//        String jobGroup = timerType.getGroupName();
//
//        if (timeout < 0) {
//            Log.error("Job timeout is less than 0, jobName={}, group={}, timeout={}.",
//                      jobName, jobGroup, timeout);
//            throw new RtException(RtErrorEnum.ERROR_INTERNAL_SERVER);
//        }
//
//        try {
//            JobDetail jobDetail = JobBuilder.newJob(timerType.getJobClass())
//                    .withIdentity(jobName, jobGroup)
//                    .build();
//
//
//            jobDetail.getJobDataMap().put(jobName, ctx);
//
//            Trigger trigger = TriggerBuilder.newTrigger()
//                    .withIdentity(jobName, jobGroup)
//                    .startAt(DateBuilder.futureDate(timeout, DateBuilder.IntervalUnit.SECOND))
//                    .build();
//
//            scheduler.scheduleJob(jobDetail, trigger);
//
//        } catch (Exception ex) {
//            Log.error("addJob failed, jobName={}, group={}, timeout={}",
//                      jobName, jobGroup, timeout, ex);
//            throw new RtException(RtErrorEnum.ERROR_INTERNAL_SERVER);
//        }
//    }
//
//
//    public void addMilliPeriodJob(ITimerSchedulerType timerType,
//                                  String jobName, int period, Object ctx) {
//        addJob(timerType, jobName, period, ctx, true);
//    }
//
//
//
//    public void addPeriodJob(ITimerSchedulerType timerType,
//                             String jobName, int period, Object ctx) {
//        addJob(timerType, jobName, period, ctx, false);
//    }
//
//
//    public void addJob(ITimerSchedulerType timerType,
//                       String jobName, int period, Object ctx, boolean isMilli) {
//        String jobGroup = timerType.getGroupName();
//        if (period < 1) {
//            Log.warn("Cron job period is too short, period={}", period);
//            throw new RtException(RtErrorEnum.ERROR_INTERNAL_SERVER);
//        }
//
//        try {
//            JobDetail jobDetail = JobBuilder.newJob(timerType.getJobClass())
//                    .withIdentity(jobName, jobGroup)
//                    .build();
//
//            jobDetail.getJobDataMap().put(jobName, ctx);
//
//            ScheduleBuilder scheduleBuilder = null;
//            if (isMilli == true) {
//                scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
//                        .withIntervalInMilliseconds(period)
//                        .repeatForever();
//            } else {
//                scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
//                        .withIntervalInSeconds(period)
//                        .repeatForever();
//            }
//
//            Trigger trigger = TriggerBuilder.newTrigger()
//                    .withIdentity(jobName, jobGroup)
//                    .startNow()
//                    //.startAt(futureDate(period, DateBuilder.IntervalUnit.SECOND))
//                    .withSchedule(scheduleBuilder)
//                    .build();
//
//            scheduler.scheduleJob(jobDetail, trigger);
//
//        } catch (SchedulerException ex) {
//            Log.error("addCronJob failed, jobName={}, group={}, period={}",
//                    jobName, jobGroup, period, ex);
//            throw new RtException(RtErrorEnum.ERROR_INTERNAL_SERVER);
//        }
//    }
//    public void addRepeatForTotalCountJob (ITimerSchedulerType timerType, String jobName,
//                                           int timeout, Object ctx, Object factory, int totalCount, int periodHour) {
//        String jobGroup = timerType.getGroupName();
//
//        if (timeout < 0) {
//            Log.error("Job timeout is less than 0, jobName={}, group={}, timeout={}.",
//                    jobName, jobGroup, timeout);
//            throw new RtException(RtErrorEnum.ERROR_INTERNAL_SERVER);
//        }
//
//        try {
//            JobDetail jobDetail = JobBuilder.newJob(timerType.getJobClass())
//                    .withIdentity(jobName, jobGroup)
//                    .build();
//
//
//            jobDetail.getJobDataMap().put(jobName, ctx);
//            jobDetail.getJobDataMap().put(jobName+ "Factory", factory);
//
//            ScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.repeatSecondlyForTotalCount(totalCount, periodHour);
//
//            Trigger trigger = TriggerBuilder.newTrigger()
//                    .withIdentity(jobName, jobGroup)
//                    .startAt(DateBuilder.futureDate(timeout, DateBuilder.IntervalUnit.SECOND))
//                    .withSchedule(scheduleBuilder)
//                    .build();
//
//            scheduler.scheduleJob(jobDetail, trigger);
//        }catch (Exception ex) {
//            Log.error("addJob failed, jobName={}, group={}, timeout={}",
//                    jobName, jobGroup, timeout, ex);
//            throw new RtException(RtErrorEnum.ERROR_INTERNAL_SERVER);
//        }
//
//    }
//
//
//    public void removeJob(ITimerSchedulerType timerType, String jobName) {
//        String jobGroup = timerType.getGroupName();
//        try {
//            JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
//            scheduler.deleteJob(jobKey);
//
//        } catch (SchedulerException ex) {
//            Log.error("addJob failed, jobName={}, group={}", jobName, jobGroup);
//            throw new RtException(RtErrorEnum.ERROR_INTERNAL_SERVER);
//        }
//    }
//}
//
